package com.accounting.client.frontend.product.handlers;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.Product;
import com.accounting.client.remote.obj.PurchaseRecordItem;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.Action;
import corelib.client.schedule.DeferredTask;
import corelib.client.search.PagingRequest;
import corelib.client.search.PagingResult;
import corelib.client.ui.SimpleModal;
import corelib.client.ui.widget.ImageProvider;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.dialog.SimpleLargeDialogHandler;
import corelib.client.ui.widget.form.SearchBox;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.panel.SimpleListPanel;
import corelib.client.ui.widget.toolbar.SimpleRibbon;

public class CreatePurchaseRecordHandler extends SimpleLargeDialogHandler {
	
	private Action action;
	private SimpleListPanel<Product> listPanel;
	
	private SearchBox box;
	private SimpleRibbon toolBar;
	private ScrollPanel summaryPanel;
	
	public CreatePurchaseRecordHandler(Action action) {
		this.action = action;
	}
	
	@Override
	public void onShowing() {
		super.onShowing();
		new DeferredTask().run(new ScheduledCommand(){
			@Override
			public void execute() {
				search(Util.EMPTY_STRING);
			}
		}, 400);
	}
	
	@Override
	public Widget getContentPanel() {
		toolBar = createToolBar();
		SimpleLayoutPanel layout = new SimpleLayoutPanel();
		SplitLayoutPanel contentPanel = createContentPanel();
		
		layout.add(toolBar);
		layout.add(contentPanel);
		layout.setWidgetTopHeight(toolBar, 0, Unit.PX, 32, Unit.PX);
		layout.setWidgetTopBottom(contentPanel, 44, Unit.PX, 0, Unit.PX);
		return layout;
	}
	
	protected SplitLayoutPanel createContentPanel() {
		SplitLayoutPanel contentPanel = new SplitLayoutPanel();
		contentPanel.getElement().getStyle().setBorderColor("#eee");
		contentPanel.getElement().getStyle().setBorderWidth(1, Unit.PX);
		listPanel = new SimpleListPanel<Product>(new WidgetListener() {
			@Override
			public void valueUpdated(Widget widget) {
				Product productInfo = listPanel.getSelectedOption();
				if (productInfo != null) {
					PurchaseRecordItem item = new PurchaseRecordItem();
					item.setPostage(productInfo.getPostage());
					item.setProductName(productInfo.getCName());
					item.setProductId(productInfo.getProductId());
					summaryPanel.setWidget(new CreatePurchaseRecordPanel(item, new Action(){
						@Override
						public void run() {
							if (action != null){
								action.run();	
							}
							closeDialog();
						}
					}));
				}
			}
		});
		listPanel.setIconProvider(new ImageProvider<Product>(){
			@Override
			public AbstractImagePrototype getImage(Product value) {
				return AbstractImagePrototype.create(BoxResources.INSTANCE.cube16());
			}
		});
		listPanel.setOptionFormatter(new ValueFormatter<Product>(){
			@Override
			public String format(Product value) {
				return value.getCName();
			}
		});
		contentPanel.addWest(listPanel, 260);
		contentPanel.add(createSummaryPanel());
		return contentPanel;
	}
	
	private ScrollPanel createSummaryPanel() {
		summaryPanel = new ScrollPanel();
		summaryPanel.getElement().getStyle().setPadding(20, Unit.PX);
		return summaryPanel;
	}

	private void search(String keyword) {
		String passport = SessionManager.getPassport();
		PagingRequest request = new PagingRequest();
		request.setOffset(0);
		request.setLimit(1000);
		request.setKeyword(keyword);
		SimpleModal.show("搜索中...");
		Services.ProductService.ListProductsByKeyword(passport, request,
			new AsyncCallback<PagingResult<Product>>(){
				@Override
				public void onFailure(Throwable caught) {
					SimpleModal.hide();
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(PagingResult<Product> result) {
					SimpleModal.hide();
					listPanel.setOptions(result.getData(), false);
				}
			}
		);
	}
	
	protected SimpleRibbon createToolBar() {
		SimpleRibbon toolBar = new SimpleRibbon();
		toolBar.getElement().getStyle().setPaddingTop(15, Unit.PX);
		box = new SearchBox(new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				new DeferredTask().run(new ScheduledCommand(){
					@Override
					public void execute() {
						search(box.getTrimmedText());
					}
				}, 1000);
			}
		});
		box.setWidth(350);
		box.setEmptyText("eg. 奶粉");
		
		Button close = new Button("关闭", BoxResources.INSTANCE.delete16(), new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				closeDialog();
			}
		});
		toolBar.addWidget(box);
		toolBar.addWidget(close, Style.Float.RIGHT);
		return toolBar;
	}
	
}
